package org.example.product.service.impl;

import java.util.List;
import java.util.ArrayList;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.example.product.mapper.CategoryMapper;
import org.example.product.domain.Category;
import org.example.product.service.ICategoryService;
import com.ruoyi.common.core.text.Convert;

/**
 * 产品分类Service业务层处理
 *
 * @author 小孟微信：xiaomengcode
 * @date 2024-04-11
 */
@Service
public class CategoryServiceImpl implements ICategoryService
{
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 查询产品分类
     *
     * @param categoryId 产品分类主键
     * @return 产品分类
     */
    @Override
    public Category selectCategoryByCategoryId(Long categoryId)
    {
        return categoryMapper.selectCategoryByCategoryId(categoryId);
    }

    /**
     * 查询产品分类列表
     *
     * @param category 产品分类
     * @return 产品分类
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<Category> selectCategoryList(Category category)
    {
        return categoryMapper.selectCategoryList(category);
    }

    /**
     * 新增产品分类
     *
     * @param category 产品分类
     * @return 结果
     */
    @Override
    public int insertCategory(Category category)
    {
        category.setCreateBy(ShiroUtils.getUserId()+"");
        category.setCreateTime(DateUtils.getNowDate());
        return categoryMapper.insertCategory(category);
    }

    /**
     * 修改产品分类
     *
     * @param category 产品分类
     * @return 结果
     */
    @Override
    public int updateCategory(Category category)
    {
        return categoryMapper.updateCategory(category);
    }

    /**
     * 批量删除产品分类
     *
     * @param categoryIds 需要删除的产品分类主键
     * @return 结果
     */
    @Override
    public int deleteCategoryByCategoryIds(String categoryIds)
    {
        return categoryMapper.deleteCategoryByCategoryIds(Convert.toStrArray(categoryIds));
    }

    /**
     * 删除产品分类信息
     *
     * @param categoryId 产品分类主键
     * @return 结果
     */
    @Override
    public int deleteCategoryByCategoryId(Long categoryId)
    {
        return categoryMapper.deleteCategoryByCategoryId(categoryId);
    }

    /**
     * 查询产品分类树列表
     *
     * @return 所有产品分类信息
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<Ztree> selectCategoryTree(Category category)
    {
        List<Category> categoryList = categoryMapper.selectCategoryList(category);
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (Category item : categoryList)
        {
            Ztree ztree = new Ztree();
            ztree.setId(item.getCategoryId());
            ztree.setpId(item.getPid());
            ztree.setName(item.getName());
            ztree.setTitle(item.getName());
            ztrees.add(ztree);
        }
        return ztrees;
    }
}
